package com.imay.live.view.custom;

import android.content.Context;
import android.content.Intent;
import android.util.SparseArray;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.live.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by  nick on 2017/5/11 0011.
 */

public class LivePermissionView extends RelativeLayout {

    public final static int TYPE_LIVE=1;
    public final static int TYPE_WEEK=2;
    public final static int TYPE_MONEY=3;

    private RadioButton rb_live_permisson;
    private LinearLayout ll_left_permission;
    private TextView tv_left_title_permisson;
    private TextView tv_left_content_permisson;
    private LinearLayout ll_centenr_permission;
    private TextView tv_centenr_title_permisson;
    private TextView tv_centenr_content_permisson;
    private LinearLayout ll_right_permission;
    private TextView tv_right_title_permisson;
    private TextView tv_right_content_permisson;


    public LivePermissionView(int level,int type,Context context) {
        super(context);
        inflate(context, R.layout.item_live_permission, this);
        bindViews();
        refreshViewByType(level,type);
    }

    private void bindViews() {
        rb_live_permisson = (RadioButton) findViewById(R.id.rb_live_permisson);
        ll_left_permission = (LinearLayout) findViewById(R.id.ll_left_permission);
        tv_left_title_permisson = (TextView) findViewById(R.id.tv_left_title_permisson);
        tv_left_content_permisson = (TextView) findViewById(R.id.tv_left_content_permisson);
        ll_centenr_permission = (LinearLayout) findViewById(R.id.ll_centenr_permission);
        tv_centenr_title_permisson = (TextView) findViewById(R.id.tv_centenr_title_permisson);
        tv_centenr_content_permisson = (TextView) findViewById(R.id.tv_centenr_content_permisson);
        ll_right_permission = (LinearLayout) findViewById(R.id.ll_right_permission);
        tv_right_title_permisson = (TextView) findViewById(R.id.tv_right_title_permisson);
        tv_right_content_permisson = (TextView) findViewById(R.id.tv_right_content_permisson);
    }

    private void refreshViewByType(int level,int type){
        int[] levels = null;
        String[] strings = null;
        switch (type){

            case TYPE_LIVE:
                 levels = getResources().getIntArray(R.array.liveLevelPermission);
                 strings = getResources().getStringArray(R.array.livePermission);
                tv_left_title_permisson.setText(R.string.permission_live);
                rb_live_permisson.setButtonDrawable(R.drawable.permission_live_selecter);

                break;
            case TYPE_WEEK:
                levels = getResources().getIntArray(R.array.weekLevelPermission);
                strings = getResources().getStringArray(R.array.weekPermission);
                tv_left_title_permisson.setText(R.string.permission_week);
                rb_live_permisson.setButtonDrawable(R.drawable.permission_week_selecter);

                break;
            case TYPE_MONEY:
                levels = getResources().getIntArray(R.array.MoneyLevelPermission);
                strings = getResources().getStringArray(R.array.MoneyPermission);
                tv_left_title_permisson.setText(R.string.permission_money);
                rb_live_permisson.setButtonDrawable(R.drawable.permission_money_selecter);

                break;
        }
        rb_live_permisson.setChecked(level>=levels[0]);
        int position = getPosition(levels,level);
        tv_left_content_permisson.setText(String.format(getResources().getString(R.string.permission_open),levels[0]));
        tv_right_title_permisson.setText(strings[position]);


        if (position==levels.length-1){
            tv_centenr_content_permisson.setText(String.format(getResources().getString(R.string.permission_max_level),level));
            tv_right_content_permisson.setText(strings[position]);
        }else{
            tv_centenr_content_permisson.setText(String.format(getResources().getString(R.string.permission_next_level),levels[position+1]));
            tv_right_content_permisson.setText(strings[position+1]);
        }


    }

    private int getPosition(int[] array,int level){
        for (int i = 0; i <array.length; i++) {
            if (level<=array[0]){
                return 0;
            }
            if (level<array[i]){
                return i-1;
            }
        }
        return array.length-1;
    }



}
